package com.bodu.common;

import lombok.Getter;
import lombok.ToString;
import org.joda.time.DateTime;

import java.io.Serializable;

/**
 * 服务端返回数据
 * @author gaoya
 */
@Getter
@ToString
public class ServResult<T> implements Serializable {
    private String code;
    private String msg;
    private DateTime time;
    @Getter
    private T rows;
    private int total;

    public boolean isSuccess() {
        return this.code.equals(StateEnum.SUCCESS.getCode());
    }


    public static ServResult success() {
        StateEnum stateEnum = StateEnum.SUCCESS;
        return new ServResult(stateEnum.getCode(), stateEnum.getMsg());
    }

    public static ServResult success(String msg) {
        StateEnum scsState = StateEnum.SUCCESS;
        return new ServResult(scsState.getCode(), msg);
    }

    public static <T> ServResult success(T data){
        StateEnum scsState = StateEnum.SUCCESS;
        return new ServResult(scsState.getCode(), scsState.getMsg(), data);
    }

    public static <T> ServResult success(String msg, T data, int count) {
        StateEnum scsState = StateEnum.SUCCESS;
        return new ServResult(scsState.getCode(), msg, data, count);
    }


    public static <T>  ServResult success(String msg, T data) {
        StateEnum scsState = StateEnum.SUCCESS;
        return new ServResult(scsState.getCode(), msg, data);
    }

    public static <T> ServResult success(String code, String msg ,T data){
        return new ServResult(code, msg, data);
    }

    public static <T>  ServResult<T> fail() {
        StateEnum fail = StateEnum.FAIL;
        return new ServResult<>(fail.getCode(), fail.getMsg());
    }

    public static <T> ServResult fail(String msg) {
        StateEnum fail = StateEnum.FAIL;
        return new ServResult(fail.getCode(), msg);
    }

    public static <T> ServResult fail(String code, String msg){
        return new ServResult(code, msg);
    }

    public ServResult(String code, String msg, T rows, int total) {
        this.code = code;
        this.msg = msg;
        this.rows = rows;
        this.total = total;
    }

    public ServResult(String code, String msg, T rows) {
        this.code = code;
        this.msg = msg;
        this.rows = rows;
        this.time = DateTime.now();
    }

    public ServResult(String msg, T rows) {
        this.msg = msg;
        this.rows = rows;
        this.time = DateTime.now();
    }

    public ServResult(String code, String msg) {
        this.code = code;
        this.msg = msg;
        this.time = DateTime.now();
    }

    public ServResult() {
    }

    @Override
    public String toString() {
        return "ServResult{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", time=" + time +
                ", rows=" + rows +
                ", total=" + total +
                '}';
    }
}
